#include "PngWidget.h"
//#include <QtCore/QtCore>

PngWidget::PngWidget(QWidget* parent):
	QWidget(parent),
	mCols(10)
{
	setAutoFillBackground(true);

	QPalette palette;

	palette.setColor(QPalette::Background, QColor(0,0,0,255));

	setPalette(palette);
}

PngWidget::~PngWidget(void)
{
}

void PngWidget::importFiles()
{
	QStringList openFiles = QFileDialog::getOpenFileNames(this, tr("Open File"), "../Resource", tr("Png Files (*.png *.jpg)"));

	if (openFiles.isEmpty())
	{
		return;
	}
// 
// 	int nSame = INT_MAX;
// 	for (int i=1; i<openFiles.size(); i++)
// 	{
// 		int nSameTmp = 0;
// 		QString str0 = openFiles[0];
// 		QString str = openFiles[i];
// 
// 		for (int n=0; n<str0.length(); n++)
// 		{
// 			if (str0.at(n) == str.at(n))
// 			{
// 				nSameTmp++;
// 			}
// 			else
// 			{
// 				if (nSameTmp < nSame)
// 				{
// 					nSame = nSameTmp;
// 				}
// 				break;
// 			}
// 		}
// 	}
// 
// 	QList<int> indexes;
// 	QMap<int,QString> fileMap;
// 	for (int i=0; i<openFiles.size(); i++)
// 	{
// 		int tmp = 0;
// 		QString str = openFiles[i];
// 
// 		str.remove(0, nSame);
// 		str.remove(".png", Qt::CaseInsensitive);
// 
// 		for (int n=str.size()-1; n>=0; n--)
// 		{
// 			if (str[n] < '0' || str[n] > '9')
// 			{
// 				str.remove(n, 1);
// 			}
// 		}
// 		str = "1" + str;
// 		fileMap[str.toInt()] = openFiles[i];
// 		indexes.push_back(str.toInt());
// 		//qSort(indexes.begin(), indexes.end());
// 	}

	if (!openFiles.isEmpty())
	{
// 		foreach(int index, indexes)
// 		{
// 			QImage image(fileMap[index]);
// 			mImages.push_back(image);
// 		}
// 		update();
		foreach(const QString& index, openFiles)
		{
			QImage image(index);
			mImages.push_back(image);
			if (mMaxSize.width() < image.width())
			{
				mMaxSize.setWidth(image.width());
			}
			if (mMaxSize.height() < image.height())
			{
				mMaxSize.setHeight(image.height());
			}
		}
		update();
	}

	resetSize();
}

void PngWidget::resetSize()
{
	if (mImages.size()>0)
	{
		QRect rt(QPoint(0,0), QSize(mMaxSize.width()*mCols, mMaxSize.height()*ceil(mImages.size()/(float)mCols)));
		resize(rt.adjusted(0,0, 5, 5).size());
	}
}

void PngWidget::paintEvent(QPaintEvent *event)
{
	QPainter painter;
	painter.begin(this);

	QPoint pos(0,0);

	int iCount=0;
	foreach(const QImage& image, mImages)
	{
		painter.drawImage(QPoint(pos.x()+(mMaxSize.width()-image.width())*0.5f, pos.y()+(mMaxSize.height()-image.height())*0.5f), image);

		if ((iCount+1)%mCols == 0)
		{
			pos.setX(0);
			pos.setY(pos.y() + mMaxSize.height());
		}
		else
		{
			pos.setX(pos.x() + mMaxSize.width());
		}

		iCount++;
		//pos.setY(pos.y() + image.size().height());
	}

	if (mImages.size() > 0)
	{
		QRect rt(QPoint(0,0), QSize(mMaxSize.width()*mCols, mMaxSize.height()*ceil(mImages.size()/(float)mCols)));

		painter.setPen(Qt::green);
		painter.drawRect(rt);

		painter.drawText(rt.topLeft() + QPoint(0, 10), QString("size:%1x%2").arg(rt.size().width()).arg(rt.size().height()));
	}

	painter.end();

}

void PngWidget::clearImage()
{
	mImages.clear();
	update();
}

void PngWidget::saveImage()
{
	QString fileName = QFileDialog::getSaveFileName(this, tr("Save File"), "../Resource", tr("Png File(*.png)"));
	if (fileName.isEmpty())
		return;

	if (mImages.size() > 0)
	{
		QRect rt(QPoint(0,0), QSize(mMaxSize.width()*mCols, mMaxSize.height()*ceil(mImages.size()/(float)mCols)));

		QImage image(rt.width(), rt.height(), mImages[0].format());
		
		QPainter painter(&image);
		painter.begin(&image);

		image.fill(0);
		
		QPoint pos(0,0);

		int iCount=0;
		foreach(const QImage& image, mImages)
		{
			painter.drawImage(QPoint(pos.x()+(mMaxSize.width()-image.width())*0.5f, pos.y()+(mMaxSize.height()-image.height())*0.5f), image);
			//painter.drawImage(pos, image);

			if ((iCount+1)%mCols == 0)
			{
				pos.setX(0);
				pos.setY(pos.y() + mMaxSize.height());
			}
			else
			{
				pos.setX(pos.x() + mMaxSize.width());
			}

			iCount++;
			//pos.setY(pos.y() + image.size().height());
		}

		painter.end();
		image.save(fileName);
	}

}

void PngWidget::colsChanged(int i)
{
	mCols = i;
	resetSize();
	update();
}